package protocol.JT808_2019;

import java.util.HashMap;

public class Handle808_2019JT06 implements IHandle808_2019JT {

    @Override
    public String handle(JT808_2019Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x08:
                return handle0608(cmd);
            default:
                return "808-2019:未匹配的命令字"+(cmd.readOrder()+","+cmd.subOrder());
        }
    }

    private String handle0608(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:查询区域线路应答");
        builder.append("\r\n");

        int type = cmd.readInt();
        HashMap<Integer,String> list = new HashMap<>();
        list.put(1,"圆形");
        list.put(2,"矩形");
        list.put(3,"多边形");
        list.put(4,"路线");

        builder.append("查询类型:" + list.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);

        for (int i = 0; i < num; i++) {
            switch (i){
                case 1:
                    handle060801(builder,cmd);
                    break;
                case 2:
                    handle060802(builder,cmd);
                    break;
                case 3:
                    handle060803(builder,cmd);
                    break;
                case 4:
                    handle060804(builder,cmd);
                    break;
            }

            builder.append("---------------------------");
        }
        return builder.toString();
    }

    private void handle060801(StringBuilder builder,JT808_2019Cmd cmd) {
        builder.append("区域ID:"+cmd.readInt(4));
        builder.append("\r\n");
        int value = cmd.readWORD();
        builder.append("属性:"+value);
        builder.append("\r\n");

        boolean isLimitTime = (value&0x01) == 1;

        boolean isLimitSpeed = (value>>1&0x01) == 1;
        builder.append("    是否启用起始时间和结束时间的判断规则(1有效):"+(value&0x01));
        builder.append("\r\n");
        builder.append("    限速(1有效):"+(value>>1&0x01));
        builder.append("\r\n");
        builder.append("    进区域报警给驾驶员(1有效)"+(value>>2&0x01));
        builder.append("\r\n");
        builder.append("    进区域报警给平台(1有效):"+(value>>3&0x01));
        builder.append("\r\n");
        builder.append("    出区域报警给驾驶员(1有效):"+(value>>4&0x01));
        builder.append("\r\n");
        builder.append("    出区域报警给平台:"+(value>>5&0x01));
        builder.append("\r\n");
        builder.append("    0-北纬,1-南纬:"+(value>>6&0x01));
        builder.append("\r\n");
        builder.append("    0-东经,1-西经:"+(value>>7&0x01));
        builder.append("\r\n");
        builder.append("    0-允许开门,1-禁止开门:"+(value>>8&0x01));
        builder.append("\r\n");
        builder.append("    0-进区域开启通信模块,1-进区域关闭通信模块:"+(value>>14&0x01));
        builder.append("\r\n");
        builder.append("    0-进区域不采集 GNSS 详细定位数据,1-进区域采集 GNSS 详细定位数据:"+(value>>15&0x01));
        builder.append("\r\n");

        builder.append("中心点纬度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("中心点经度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("半径:"+cmd.readDWORD());
        builder.append("\r\n");

        if (isLimitTime){
            builder.append("起始时间:"+cmd.readBCD(6));
            builder.append("\r\n");
            builder.append("结束时间:"+cmd.readBCD(6));
            builder.append("\r\n");
        }

        if (isLimitSpeed){
            builder.append("最高速度（超速限值）:"+cmd.readWORD());
            builder.append("\r\n");

            builder.append("超速持续时间:"+cmd.readInt());
            builder.append("\r\n");

            builder.append("夜间最高速度(千米每小时):"+cmd.readWORD());
            builder.append("\r\n");
        }

        builder.append("区域名称:"+cmd.readString(cmd.readWORD()));
        builder.append("\r\n");
    }

    private void handle060802(StringBuilder builder,JT808_2019Cmd cmd) {
        builder.append("区域ID:"+cmd.readInt(4));
        builder.append("\r\n");
        int value = cmd.readWORD();
        builder.append("属性:"+value);
        builder.append("\r\n");

        boolean isLimitTime = (value&0x01) == 1;

        boolean isLimitSpeed = (value>>1&0x01) == 1;
        builder.append("    是否启用起始时间和结束时间的判断规则(1有效):"+(value&0x01));
        builder.append("\r\n");
        builder.append("    限速(1有效):"+(value>>1&0x01));
        builder.append("\r\n");
        builder.append("    进区域报警给驾驶员(1有效)"+(value>>2&0x01));
        builder.append("\r\n");
        builder.append("    进区域报警给平台(1有效):"+(value>>3&0x01));
        builder.append("\r\n");
        builder.append("    出区域报警给驾驶员(1有效):"+(value>>4&0x01));
        builder.append("\r\n");
        builder.append("    出区域报警给平台:"+(value>>5&0x01));
        builder.append("\r\n");
        builder.append("    0-北纬,1-南纬:"+(value>>6&0x01));
        builder.append("\r\n");
        builder.append("    0-东经,1-西经:"+(value>>7&0x01));
        builder.append("\r\n");
        builder.append("    0-允许开门,1-禁止开门:"+(value>>8&0x01));
        builder.append("\r\n");

        builder.append("    0-进区域开启通信模块,1-进区域关闭通信模块:"+(value>>14&0x01));
        builder.append("\r\n");

        builder.append("    0-进区域不采集 GNSS 详细定位数据,1-进区域采集 GNSS 详细定位数据:"+(value>>15&0x01));
        builder.append("\r\n");

        builder.append("左上点纬度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("左上点经度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("右下点纬度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("右下点经度:"+cmd.readInt(4));
        builder.append("\r\n");

        if (isLimitTime){
            builder.append("起始时间:"+cmd.readBCD(6));
            builder.append("\r\n");
            builder.append("结束时间:"+cmd.readBCD(6));
            builder.append("\r\n");
        }

        if (isLimitSpeed){
            builder.append("最高速度（超速限值，千米每小时）:"+cmd.readWORD());
            builder.append("\r\n");

            builder.append("超速持续时间（秒）:"+cmd.readInt());
            builder.append("\r\n");

            builder.append("夜间最高速度(千米每小时):"+cmd.readWORD());
            builder.append("\r\n");
        }

        builder.append("区域名称:"+cmd.readString(cmd.readWORD()));
        builder.append("\r\n");
    }

    private void handle060803(StringBuilder builder,JT808_2019Cmd cmd) {
        builder.append("区域ID:"+cmd.readDWORD());
        builder.append("\r\n");
        int value = cmd.readWORD();
        builder.append("    属性:"+value);
        builder.append("\r\n");

        boolean isLimitTime = (value&0x01) == 1;

        boolean isLimitSpeed = (value>>1&0x01) == 1;
        builder.append("    是否启用起始时间和结束时间的判断规则(1有效):"+(value&0x01));
        builder.append("\r\n");
        builder.append("    限速(1有效):"+(value>>1&0x01));
        builder.append("\r\n");
        builder.append("    进区域报警给驾驶员(1有效)"+(value>>2&0x01));
        builder.append("\r\n");
        builder.append("    进区域报警给平台(1有效):"+(value>>3&0x01));
        builder.append("\r\n");
        builder.append("    出区域报警给驾驶员(1有效):"+(value>>4&0x01));
        builder.append("\r\n");
        builder.append("    出区域报警给平台:"+(value>>5&0x01));
        builder.append("\r\n");
        builder.append("    0-北纬,1-南纬:"+(value>>6&0x01));
        builder.append("\r\n");
        builder.append("    0-东经,1-西经:"+(value>>7&0x01));
        builder.append("\r\n");
        builder.append("    0-允许开门,1-禁止开门:"+(value>>8&0x01));
        builder.append("\r\n");
        builder.append("    0-进区域开启通信模块,1-进区域关闭通信模块:"+(value>>14&0x01));
        builder.append("\r\n");
        builder.append("    0-进区域不采集 GNSS 详细定位数据,1-进区域采集 GNSS 详细定位数据:"+(value>>15&0x01));
        builder.append("\r\n");

        if (isLimitTime){
            builder.append("起始时间:"+cmd.readBCD(6));
            builder.append("\r\n");
            builder.append("结束时间:"+cmd.readBCD(6));
            builder.append("\r\n");
        }

        if (isLimitSpeed){
            builder.append("    最高速度（超速限值，千米每小时）:"+cmd.readWORD());
            builder.append("\r\n");

            builder.append("    超速持续时间（秒）:"+cmd.readInt());
            builder.append("\r\n");
        }

        int pointNum = cmd.readWORD();
        builder.append("顶点数量:"+pointNum);
        builder.append("\r\n");
        for (int i = 0; i < pointNum; i++) {
            builder.append("    顶点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    顶点经度:"+cmd.readInt(4));
            builder.append("\r\n");
        }

        if (isLimitSpeed){
            builder.append("夜间最高速度(千米每小时):"+cmd.readWORD());
            builder.append("\r\n");
        }

        builder.append("区域名称:"+cmd.readString(cmd.readWORD()));
        builder.append("\r\n");
    }


    private void handle060804(StringBuilder builder,JT808_2019Cmd cmd){

        builder.append("\r\n");
        builder.append("-------------------------------");
        builder.append("\r\n");
        builder.append("线路ID:"+cmd.readDWORD());
        builder.append("\r\n");
        int value = cmd.readWORD();
        builder.append("线路属性:"+value);
        builder.append("\r\n");

        boolean isByTime = (value&0x01) == 1;
        builder.append("    根据时间(1有效):"+(value&0x01));
        builder.append("\r\n");
        builder.append("    进线路报警给驾驶员(1有效)"+(value>>2&0x01));
        builder.append("\r\n");
        builder.append("    进线路报警给平台(1有效):"+(value>>3&0x01));
        builder.append("\r\n");
        builder.append("    出线路报警给驾驶员(1有效):"+(value>>4&0x01));
        builder.append("\r\n");
        builder.append("    出线路报警给平台:"+(value>>5&0x01));
        builder.append("\r\n");

        if (isByTime){
            builder.append("起始时间:"+cmd.readBCD(6));
            builder.append("\r\n");
            builder.append("结束时间:"+cmd.readBCD(6));
            builder.append("\r\n");
        }

        int num = cmd.readWORD();
        builder.append("路线拐点项数:"+num);
        builder.append("\r\n");
        for (int i = 0; i < num; i++) {
            builder.append("    拐点ID:"+cmd.readDWORD());
            builder.append("\r\n");
            builder.append("    路段ID:"+cmd.readDWORD());
            builder.append("\r\n");

            builder.append("    拐点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    拐点经度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    路段宽度:"+cmd.readInt());
            builder.append("\r\n");
            int value2 = cmd.readWORD();
            builder.append("    路段属性:"+value2);
            builder.append("\r\n");

            boolean isLimitTime = (value&0x01) == 1;
            boolean isLimitSpeed = (value>>1&0x01) == 1;
            builder.append("        根据时间(1有效):"+(value&0x01));
            builder.append("\r\n");
            builder.append("        限速(1有效):"+(value>>1&0x01));
            builder.append("\r\n");
            builder.append("        0-北纬,1-南纬:"+(value>>2&0x01));
            builder.append("\r\n");
            builder.append("        0-东经,1-西经:"+(value>>3&0x01));
            builder.append("\r\n");

            if (isLimitTime){
                builder.append("    路段行驶过长阈值:"+cmd.readWORD());
                builder.append("\r\n");

                builder.append("    路段行驶不足阈值:"+cmd.readWORD());
                builder.append("\r\n");
            }

            if (isLimitSpeed){
                builder.append("    最高速度（超速限值）:"+cmd.readWORD());
                builder.append("\r\n");

                builder.append("    路段超速持续时间:"+cmd.readWORD());
                builder.append("\r\n");

                builder.append("    夜间最高速度(千米每小时):"+cmd.readWORD());
                builder.append("\r\n");
            }
        }

        builder.append("路线名称:"+cmd.readString(cmd.readWORD()));
        builder.append("\r\n");
    }
}
